<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="logo-img">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统 </span>
        </div>
        <el-button @click="btn" type="info" size="mini">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="flag ? '64px' : '200px'">
          <div data-v-a9586ab6="" class="toggle-button" @click="togglebutton">
            |||
          </div>
          <el-menu
            :router="true"
            :unique-opened="true"
            :default-active="defaultActive"
            :collapse="flag"
            :collapse-transition="false"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
          >
            <el-submenu
              :index="'/' + index + ''"
              v-for="(item, index) in menuList"
              :key="index"
            >
              <template slot="title">
                <i
                  style="margin-right:10px"
                  :class="['iconfont', iconObj[index]]"
                ></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  :index="item2.path"
                  v-for="(item2, index2) in item.children"
                  :key="index2"
                  @click="itemHandle(item2.path)"
                >
                  <i style="margin-right:10px" class=" el-icon-menu"></i
                  >{{ item2.authName }}
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main><router-view></router-view></el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { menus_api } from "@/api";
export default {
  data() {
    return {
      menuList: [],
      //  字体图标 类名集合
      iconObj: {
        0: "icon-user",
        1: "icon-tijikongjian",
        2: "icon-shangpin",
        3: "icon-danju",
        4: "icon-baobiao"
      },
      //  默认展开哪一项
      //一打开页面时 先直接去本地拿  本地没有就显示默认的path值

      defaultActive: window.localStorage.getItem("defaultActive") || "users",
      //  控制菜单栏收起还是打开
      // collapse: false
      flag: false
    };
  },
  methods: {
    //退出按钮
    btn() {
      //清除token  调转登录页面
      window.sessionStorage.removeItem("token");
      this.$router.push("/Login");
    },
    //  获取左侧菜单函数
    async getMenus() {
      const res1 = await menus_api();
      //  发送请求获取左侧列表
      // console.log(res1.data, 22222222);
      this.menuList = res1.data.data;
      // console.log(this.menuList.data, 777777777);
      // console.log(this.menuList, 777777777);
    },
    //点击二级菜单事件  接收传过来的每一项的path值-
    //1-将获取的path赋值给defaultActive
    // 2-将这个值做本地缓存   localStorage
    // 3-data中的defaultActive  初次进入或者刷新的时候  从本地缓存中拿，如果本地缓存有值就直接拿，没值的话就取默认值 users
    itemHandle(path) {
      this.defaultActive = path;
      localStorage.setItem("defaultActive", this.defaultActive);

      // console.log(path, 99999);
    },
    togglebutton() {
      this.flag = !this.flag;
    }
  },
  mounted() {
    this.getMenus();
  }
};
</script>
<style scoped lang="less">
.home-container {
  > .el-container {
    width: 100%;
    height: 100%;
    .el-header {
      display: flex;
      justify-content: space-between;
      padding-left: 0;
      background-color: #373d41;
      .logo-img {
        display: flex;
        align-items: center;

        span {
          color: #fff;
          font-size: 20px;
          margin-left: 20px;
        }
      }

      .el-button {
        height: 40px;
        margin: 10px 0;
        font-size: 15px;
      }
    }
    .el-container {
      flex: 1;
      .el-aside {
        background-color: #373d41;
      }

      .el-main {
        background-color: #eaedf1;
      }
      .el-menu {
        border: none;
      }
      .toggle-button[data-v-a9586ab6] {
        background-color: #4a5064;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
    }
  }
}
</style>
